package com.hiwan.system.web.data;

import java.sql.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hiwan.platform.util.DateUtil;
import com.hiwan.system.pojo.DataInfo;
import com.hiwan.system.pojo.DataViewAuth;
import com.hiwan.system.service.data.DataService;
import com.hiwan.system.util.UserCacheUtils;

@Controller
@RequestMapping("/data")
public class DataController{
	
	@Autowired
	private DataService dataService;

	@RequestMapping("data.do")
	public String go(DataInfo dataApplyInfo, Model model) {
		return "data/dataIndex";
	}
	
	@RequestMapping("toAddPage.do")
	public String toAddPage(DataInfo dataInfo,DataViewAuth dataViewAuth,HttpSession session,ModelMap model){
		
		dataInfo.setApplyEmp(UserCacheUtils.getUser().getUserId());
		dataInfo.setApplyArea(UserCacheUtils.getOrgId());
		dataInfo.setApplyDept(UserCacheUtils.getDeptId());
		dataInfo.setApplyDate(DateUtil.getDateString(new Date(System.currentTimeMillis()),"yyyy-MM-dd"));
		dataInfo.setPreApplyState("0");
		dataInfo.setApplyState("0");
		
		String busiId = dataService.getBusiId(UserCacheUtils.getOrgId());
		dataInfo.setBusiId(busiId);
		
		model.put("flag", "0");//0表示新增
		model.put("dataInfo", dataInfo);
		return "data/dataDetail";
	}
	
	@RequestMapping("/dataInfoList.do")
	public String selectDataList(String applyDate,String applyState, ModelMap model) {
		DataInfo dataInfo = new DataInfo();
		dataInfo.setApplyDate(applyDate);
		dataInfo.setApplyState(applyState);
		List<DataInfo> dataInfoList = dataService.selectDataInfoList(dataInfo);
		model.put("dataInfoList", dataInfoList);
		return "data/dataInfoList";
	}
	
	@RequestMapping("detail.do")
	public String detail(String busiId,String flag,ModelMap model){
		DataInfo dataInfo = dataService.selectDataInfoById(busiId);
		model.put("dataInfo", dataInfo);
		model.put("flag", flag);
		return "data/dataDetail";
	}
		
	/**
	 * 保存信息
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("insert.do")
	public @ResponseBody String[] insert(DataInfo dataInfo,DataViewAuth dataViewAuth,HttpSession session,ModelMap model){
		String[] data;
		int result;
		try {
			 result =dataService.insertDataInfo(dataInfo,dataViewAuth,session,null);
		} catch (Exception e) {
			e.printStackTrace();
			model.put("flag", dataInfo.getApplyState());
			data = new String[]{"fail","保存失败"};
			return data;
		}
		model.put("flag", dataInfo.getApplyState());
		if (result == 1) {
			data = new String[] { "ok", "保存成功！" };
		} else {
			data = new String[] { "fail", "保存失败！" };
		}
		return data;
	}
	
	@RequestMapping("update.do")
	public @ResponseBody String[] update(DataInfo dataInfo,DataViewAuth dataViewAuth,ModelMap model,HttpSession session){
		String[] data;
		int result;
		try {
			 result =dataService.updateDataInfo(dataInfo,dataViewAuth,session);
		} catch (Exception e) {
			e.printStackTrace();
			model.put("flag", dataInfo.getApplyState());
			data = new String[]{"fail","保存失败"};
			return data;
		}
		
		model.put("flag", dataInfo.getApplyState());
		if (result == 1) {
			data = new String[] { "ok", "保存成功！" };
		} else {
			data = new String[] { "fail", "保存失败！" };
		}
		return data;
	}
	
	@RequestMapping("delete.do")
	public @ResponseBody String[] delete(DataInfo dataInfo){
		int result =dataService.deleteDataInfo(dataInfo.getBusiId());
		
		String[] data;
		if (result == 1) {
			data = new String[] { "ok", "删除成功！" };
		} else {
			data = new String[] { "fail", "删除失败！" };
		}
		return data;
	}
}
